﻿using HardcoreGame.Common.Systems;
using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.GeomagneticStorm
{
    public class PolarizedUFO : ModNPC
    {
        private int buffTimer = 0;
        private const int BuffInterval = 60 * 15;
        private const int BuffDuration = 60 * 30;
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[Type] = 4;
        }
        public override void SetDefaults()
        {
            NPC.width = 50;
            NPC.height = 30;
            NPC.lifeMax = 100;
            NPC.damage = 60;
            NPC.defense = 25;
            NPC.HitSound = SoundID.NPCHit4;
            NPC.DeathSound = SoundID.NPCDeath14;
            NPC.value = 20f;
            NPC.knockBackResist = 0.3f;
            NPC.noGravity = true;
            NPC.noTileCollide = true;
            NPC.aiStyle = 64;
            AIType = 355;
            SpawnModBiomes = [ModContent.GetInstance<Biomes.GeomagneticStorm>().Type];
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Surface,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.Bestiary.PolarizedUFO")
            ]);
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (EventSystem.activeEvents.Contains(3) && spawnInfo.Player.ZoneForest)
            {
                return 0.5f;
            }
            return 0f;
        }
        public override void ApplyDifficultyAndPlayerScaling(int numPlayers, float balance, float bossAdjustment)
        {
            NPC.lifeMax = NPC.lifeMax;
            NPC.damage = NPC.damage;
        }
        public override void FindFrame(int frameHeight)
        {
            NPC.frameCounter += 0.15000000596046448;
            NPC.frameCounter %= Main.npcFrameCount[NPC.type];
            int frame = (int)NPC.frameCounter;
            NPC.frame.Y = frame * frameHeight;
        }
        public override void AI()
        {
            Lighting.AddLight((int)((NPC.position.X + NPC.width / 2) / 16f), (int)((NPC.position.Y + NPC.height / 2) / 16f), 0.4f, 0.4f, 0.4f);
            buffTimer++;
            if (buffTimer >= BuffInterval)
            {
                ApplyControlFlip();
                buffTimer = 0;
            }
        }
        private void ApplyControlFlip()
        {
            foreach (Player player in Main.player)
            {
                if (player.active && !player.dead && Vector2.Distance(player.Center, NPC.Center) < 500f && !player.HasBuff<ControlFlip>())
                {
                    player.AddBuff(ModContent.BuffType<ControlFlip>(), BuffDuration);
                    for (int i = 0; i < 20; i++)
                    {
                        Dust.NewDustPerfect(
                            player.Center,
                            DustID.Electric,
                            Main.rand.NextVector2Circular(3f, 3f),
                            0,
                            new Color(100, 200, 255),
                            1.5f
                        );
                    }
                }
            }
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                }
                return;
            }
            int num = 0;
            while (num < hit.Damage / (double)NPC.lifeMax * 50.0)
            {
                Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, hit.HitDirection, -1f, 0, default, 0.8f);
                num++;
            }
        }
    }
}
